home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / Kant Pro source Folder / Kant Pro 1.1 ƒ / Shell ƒ / text twiddling.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-23  |  9.7 KB  |  473 lines  |  [TEXT/MMCC]

  1. #include "text twiddling.h"
  2. #include "window layer.h"
  3.  
  4. #define RequireWindow(x)    if (theWindow==0L) return x
  5. #define RequireTE(x)        {    \
  6.                                 hTE=GetWindowTE(theWindow);    \
  7.                                 if (hTE==0L)    return x;    }
  8.  
  9. Boolean AnyTextInScrapQQ(void)
  10. {
  11.     long            dummy;
  12.     
  13.     LoadScrap();
  14.     return (GetScrap(0L, 'TEXT', &dummy)!=noTypeErr);
  15. }
  16.  
  17. void SetTheText(WindowPtr theWindow, Ptr data, long count)
  18. {
  19.     TEHandle        hTE;
  20.     ControlHandle    vScrollBar;
  21.     
  22.     RequireWindow();
  23.     RequireTE();
  24.     vScrollBar=GetWindowVScrollBar(theWindow);
  25.     
  26.     TESetText(data, count, hTE);
  27.     TESetSelect(0, 0, hTE);
  28.     if (vScrollBar!=0L)
  29.         AdjustVScrollBar(vScrollBar, hTE);
  30. }
  31.  
  32. Boolean AnyTextQQ(WindowPtr theWindow)
  33. {
  34.     TEHandle        hTE;
  35.     
  36.     RequireWindow(FALSE);
  37.     RequireTE(FALSE);
  38.     
  39.     return ((**hTE).teLength!=0);
  40. }
  41.  
  42. Boolean AnyHighlightedQQ(WindowPtr theWindow)
  43. {
  44.     TEHandle        hTE;
  45.     
  46.     RequireWindow(FALSE);
  47.     RequireTE(FALSE);
  48.     
  49.     return ((**hTE).selStart!=(**hTE).selEnd);
  50. }
  51.  
  52. short SelectionStart(WindowPtr theWindow)
  53. {
  54.     TEHandle        hTE;
  55.     
  56.     RequireWindow(-1);
  57.     RequireTE(-1);
  58.     
  59.     return (**hTE).selStart;
  60. }
  61.  
  62. short SelectionEnd(WindowPtr theWindow)
  63. {
  64.     TEHandle        hTE;
  65.     
  66.     RequireWindow(-1);
  67.     RequireTE(-1);
  68.     
  69.     return (**hTE).selEnd;
  70. }
  71.  
  72. Boolean InsertBeforeStart(WindowPtr theWindow, Str255 theStr)
  73. {
  74.     TEHandle        hTE;
  75.     unsigned long    len;
  76.     unsigned long    oldLen;
  77.     
  78.     RequireWindow(FALSE);
  79.     RequireTE(FALSE);
  80.     
  81.     len=theStr[0];
  82.     hTE=GetWindowTE(theWindow);
  83.     oldLen=(**hTE).teLength;
  84.     if (oldLen+len>32767)
  85.         return FALSE;
  86.     
  87.     TEInsert(&theStr[1], len, hTE);
  88.     SetWindowIsModified(theWindow, TRUE);
  89.     return TRUE;
  90. }
  91.  
  92. Boolean InsertAfterEnd(WindowPtr theWindow, Str255 theStr)
  93. {
  94.     TEHandle        hTE;
  95.     unsigned long    len;
  96.     unsigned long    oldLen;
  97.     unsigned long    oldSelStart;
  98.     unsigned long    oldSelEnd;
  99.     
  100.     RequireWindow(FALSE);
  101.     RequireTE(FALSE);
  102.     
  103.     len=theStr[0];
  104.     hTE=GetWindowTE(theWindow);
  105.     oldLen=(**hTE).teLength;
  106.     if (oldLen+len>32767)
  107.         return FALSE;
  108.     
  109.     oldSelStart=(**hTE).selStart;
  110.     oldSelEnd=(**hTE).selEnd;
  111.     TESetSelect(oldSelEnd, oldSelEnd, hTE);
  112.     TEInsert(&theStr[1], len, hTE);
  113.     TESetSelect(oldSelStart, oldSelEnd, hTE);
  114.     SetWindowIsModified(theWindow, TRUE);
  115.     return TRUE;
  116. }
  117.  
  118. short TotalNumberOfLines(TEHandle hTE)
  119. {
  120.     short            numLines;
  121.     
  122.     if (hTE==0L)
  123.         return -1;
  124.     
  125.     numLines=(**hTE).nLines;
  126.     if (*((unsigned char*)((long)(*((**hTE).hText))+(**hTE).teLength-1))==0x0d)
  127.         numLines++;
  128.     
  129.     return numLines;
  130. }
  131.  
  132. pascal void ScrollActionProc(ControlHandle theHandle, short partCode)
  133. {
  134.     short            scrollDistance;
  135.     TEHandle        hTE;
  136.     WindowPtr        theWindow;
  137.     
  138.     theWindow=(**theHandle).contrlOwner;
  139.     if (theWindow==0L)
  140.         return;
  141.     
  142.     hTE=GetWindowTE(theWindow);
  143.     if (hTE==0L)
  144.         return;
  145.     
  146.     switch (partCode)
  147.     {
  148.         case inUpButton:
  149.         case inDownButton:
  150.             scrollDistance=(**hTE).lineHeight;
  151.             break;
  152.         case inPageUp:
  153.         case inPageDown:
  154.             scrollDistance=(**hTE).viewRect.bottom-(**hTE).viewRect.top-(**hTE).lineHeight;
  155.             break;
  156.         default:
  157.             scrollDistance=0;
  158.             break;
  159.     }
  160.     
  161.     if ((partCode==inDownButton) || (partCode==inPageDown))
  162.         scrollDistance=-scrollDistance;
  163.     
  164.     MyMoveScrollBox(theHandle, scrollDistance/(**hTE).lineHeight);
  165.     
  166.     if (scrollDistance!=0)
  167.     {
  168.         TEPinScroll(0, scrollDistance, hTE);
  169.     }
  170. }
  171.  
  172. pascal void HScrollActionProc(ControlHandle theHandle, short partCode)
  173. {
  174.     short            scrollDistance;
  175.     TEHandle        hTE;
  176.     WindowPtr        theWindow;
  177.     
  178.     theWindow=(**theHandle).contrlOwner;
  179.     if (theWindow==0L)
  180.         return;
  181.     
  182.     hTE=GetWindowTE(theWindow);
  183.     if (hTE==0L)
  184.         return;
  185.     
  186.     switch (partCode)
  187.     {
  188.         case inUpButton:
  189.         case inDownButton:
  190.             scrollDistance=20;
  191.             break;
  192.         case inPageUp:
  193.         case inPageDown:
  194.             scrollDistance=(**hTE).viewRect.right-(**hTE).viewRect.left-20;
  195.             break;
  196.         default:
  197.             scrollDistance=0;
  198.             break;
  199.     }
  200.     
  201.     if ((partCode==inDownButton) || (partCode==inPageDown))
  202.         scrollDistance=-scrollDistance;
  203.     
  204.     MyMoveScrollBox(theHandle, scrollDistance);
  205.     
  206.     if (scrollDistance!=0)
  207.     {
  208.         TEPinScroll(scrollDistance, 0, hTE);
  209.     }
  210. }
  211.  
  212. void MyMoveScrollBox(ControlHandle theControl, short scrollDistance)
  213. {
  214.     short            oldSetting, setting, max;
  215.     
  216.     oldSetting=GetControlValue(theControl);
  217.     max=GetControlMaximum(theControl);
  218.     setting=oldSetting-scrollDistance;
  219.     if (setting<0)
  220.         setting=0;
  221.     else if (setting>max)
  222.         setting=max;
  223.     
  224.     SetControlValue(theControl, setting);
  225. }
  226.  
  227. void AdjustVScrollBar(ControlHandle theControl, TEHandle hTE)
  228. {
  229.     short            oldMax, oldValue;
  230.     short            numLines, max, value;
  231.     
  232.     oldMax=GetControlMaximum(theControl);
  233.     oldValue=GetControlValue(theControl);
  234.     numLines=TotalNumberOfLines(hTE);
  235.     max=numLines-(((**hTE).viewRect.bottom-(**hTE).viewRect.top)/((**hTE).lineHeight));
  236.     if (max<0)
  237.         max=0;
  238.     SetControlMaximum(theControl, max);
  239.     value=((**hTE).viewRect.top-(**hTE).destRect.top)/((**hTE).lineHeight);
  240.     if (value<0)
  241.         value=0;
  242.     else if (value>max)
  243.         value=max;
  244.     SetControlValue(theControl, value);
  245.     if ((oldMax!=max) || (oldValue!=value))
  246.         ShowControl(theControl);
  247. }
  248.  
  249. short CurrentLineNumber(TEHandle hTE)
  250. {
  251.     if (hTE==0L)
  252.         return -1;
  253.     
  254.     return LineNumberFromOffset(hTE, (**hTE).selStart);
  255. }
  256.  
  257. short LineNumberFromOffset(TEHandle hTE, short offset)
  258. {
  259.     short            i;
  260.     short            lineNumber;
  261.     short            numLines;
  262.     
  263.     if (hTE==0L)
  264.         return -1;
  265.     
  266.     lineNumber=-1;
  267.     numLines=(**hTE).nLines;
  268.     for (i=0; i<numLines; i++)
  269.     {
  270.         if ((**hTE).lineStarts[i]<=offset)
  271.             lineNumber++;
  272.         else
  273.             return lineNumber;
  274.     }
  275.     
  276.     return numLines-1;
  277. }
  278.  
  279. short LineStart(TEHandle hTE, short lineNum)
  280. {
  281.     if (hTE==0L)
  282.         return -1;
  283.     
  284.     return (**hTE).lineStarts[lineNum];
  285. }
  286.  
  287. pascal Boolean MyClikLoop(void)
  288. {
  289.     Point            thePoint;
  290.     TEHandle        hTE;
  291.     WindowPtr        theWindow;
  292.     GrafPtr            curPort;
  293.     
  294.     theWindow=GetFrontDocumentWindow();
  295.     if (theWindow==0L)
  296.         theWindow=FrontWindow();
  297.     if (theWindow==0L)
  298.         return FALSE;
  299.     
  300.     hTE=GetWindowTE(theWindow);
  301.     if (hTE==0L)
  302.         return FALSE;
  303.     
  304.     GetPort(&curPort);
  305.     SetPort(theWindow);
  306.     GetMouse(&thePoint);
  307.     if (!PtInRect(thePoint, &((**hTE).viewRect)))
  308.     {
  309.         if (thePoint.v<((**hTE).viewRect.top))
  310.         {
  311.             if ((**hTE).selStart>0)
  312.                 TEPinScroll(0, (**hTE).lineHeight, hTE);
  313.         }
  314.         else if (thePoint.v>(**hTE).viewRect.bottom)
  315.         {
  316.             if ((**hTE).selEnd<(**hTE).teLength)
  317.                 TEPinScroll(0, -(**hTE).lineHeight, hTE);
  318.         }
  319.         
  320.         ClickLoopAddOn(theWindow);
  321.     }
  322.     
  323.     SetPort(curPort);
  324.     
  325.     return TRUE;
  326. }
  327.  
  328. void ClickLoopAddOn(WindowPtr theWindow)
  329. {
  330.     RgnHandle        rgnHandle;
  331.     
  332.     rgnHandle=NewRgn();
  333.     GetClip(rgnHandle);
  334.     ClipRect(&(theWindow->portRect));
  335.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), GetWindowTE(theWindow));
  336.     SetClip(rgnHandle);
  337.     DisposeRgn(rgnHandle);
  338. }
  339.  
  340. void AdjustForEndScroll(ControlHandle theControl, TEHandle hTE)
  341. {
  342.     short            numLines;
  343.     short            offset;
  344.     
  345.     if (hTE==0L)
  346.         return;
  347.     
  348.     numLines=TotalNumberOfLines(hTE);
  349.     offset=numLines-GetControlMaximum(theControl)-
  350.         (((**hTE).viewRect.bottom-(**hTE).viewRect.top)/(**hTE).lineHeight);
  351.     if (offset<0)
  352.         TEPinScroll(0, -offset*((**hTE).lineHeight), hTE);
  353. }
  354.  
  355. #define kTextMargin    2
  356.  
  357. /* Return a rectangle that is inset from the portRect by the size of
  358.     the scrollbars and a little extra margin. */
  359.  
  360. void GetTERect(WindowPtr theWindow, Rect *teRect, Boolean adjustForScrollBars)
  361. {
  362.     RequireWindow();
  363.     
  364.     *teRect = theWindow->portRect;
  365.     InsetRect(teRect, kTextMargin, kTextMargin);    /* adjust for margin */
  366.     if (adjustForScrollBars)
  367.     {
  368.         teRect->bottom = teRect->bottom - 15;        /* and for the scrollbars */
  369.         teRect->right = teRect->right - 15;
  370.     }
  371. }
  372.  
  373.  
  374. /* Update the TERec's view rect so that it is the greatest multiple of
  375.     the lineHeight that still fits in the old viewRect. */
  376.  
  377. void AdjustViewRect(TEHandle docTE)
  378. {
  379.     TEPtr        te;
  380.     
  381.     if (docTE==0L)
  382.         return;
  383.     
  384.     te = *docTE;
  385.     te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
  386.                             * te->lineHeight) + te->viewRect.top;
  387. }
  388.  
  389.  
  390. /*    Re-calculate the position and size of the viewRect and the scrollbars.
  391.     kScrollTweek compensates for off-by-one requirements of the scrollbars
  392.     to have borders coincide with the growbox. */
  393.  
  394. #define kScrollbarWidth        16
  395. #define kScrollbarAdjust    (kScrollbarWidth-1)
  396. #define kScrollTweek        2
  397.  
  398. void AdjustScrollSizes(WindowPtr window, TEHandle hTE, ControlHandle vScrollBar,
  399.     ControlHandle hScrollBar, short destOverload)
  400. {
  401.     Rect        teRect;
  402.     
  403.     if (window==0L)
  404.         return;
  405.     if (hTE==0L)
  406.         return;
  407.     
  408.     GetTERect(window, &teRect, TRUE);                            /* start with TERect */
  409.     (**hTE).viewRect = teRect;
  410.     (**hTE).destRect.left=(**hTE).viewRect.left;
  411.     (**hTE).destRect.right=(**hTE).viewRect.right+destOverload;
  412.     MoveControl(vScrollBar, window->portRect.right - kScrollbarAdjust, -1);
  413.     SizeControl(vScrollBar, kScrollbarWidth, (window->portRect.bottom - 
  414.                 window->portRect.top) - (kScrollbarAdjust - kScrollTweek));
  415.     MoveControl(hScrollBar, -1, window->portRect.bottom - kScrollbarAdjust);
  416.     SizeControl(hScrollBar, (window->portRect.right - 
  417.                 window->portRect.left) - (kScrollbarAdjust - kScrollTweek),
  418.                 kScrollbarWidth);
  419. }
  420.  
  421. void AdjustTE(TEHandle hTE, ControlHandle vScrollBar, ControlHandle hScrollBar)
  422. {
  423.     TEPtr        te;
  424.     
  425.     if (hTE==0L)
  426.         return;
  427.     if (vScrollBar==0L)
  428.         return;
  429.     if (hScrollBar==0L)
  430.         return;
  431.     
  432.     te = *hTE;
  433.     TEScroll((te->viewRect.left - te->destRect.left) -
  434.             GetControlValue(hScrollBar),
  435.             ((te->viewRect.top - te->destRect.top)/te->lineHeight) -
  436.                 GetControlValue(vScrollBar),
  437.             hTE);
  438. }
  439.  
  440. void DrawTheShadowBox(Rect theRect, Boolean eraseBackground)
  441. {
  442.     theRect.right-=2;
  443.     theRect.bottom-=2;
  444.     if (eraseBackground)
  445.         EraseRect(&theRect);
  446.     FrameRect(&theRect);
  447.     MoveTo(theRect.left+3, theRect.bottom+1);
  448.     Line(theRect.right-theRect.left-2, 0);
  449.     Line(0, -theRect.bottom+theRect.top+3);
  450.     MoveTo(theRect.left+3, theRect.bottom);
  451.     Line(theRect.right-theRect.left-3, 0);
  452.     Line(0, -theRect.bottom+theRect.top+4);
  453. }
  454.  
  455. /* Note: this PowerMac click loop procedure is not correct.  This will not run properly if you
  456.    compile and run it for native PowerMac. -MP, 1/23/95 */
  457.  
  458. #if powerc
  459. pascal Boolean PPCClickLoopProc(TEPtr pTE)
  460. {
  461.     WindowPtr        theWindow;
  462.     
  463.     theWindow=GetFrontDocumentWindow();
  464.     if (theWindow==0L)
  465.         theWindow=FrontWindow();
  466.     if (theWindow==0L)
  467.         return FALSE;
  468.     CallTEClickLoopProc(GetWindowOldClickLoopProc(theWindow), pTE);
  469.     ClickLoopAddOn(theWindow);
  470.     return TRUE;
  471. }
  472. #endif
  473.